package com.example.librarymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.librarymanagement.dao.RoleMapper;
import com.example.librarymanagement.dto.role.RoleDto;
import com.example.librarymanagement.entity.Role;
import com.example.librarymanagement.service.IRoleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author fgba
 * @since 2021-05-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public boolean addOrUpdRole(RoleDto roleDto) {
        Role role = new Role();
        roleDto.convertToEntity(role);
        if (role.getId() != null) {
            role.setUpdated(LocalDateTime.now());
            return updateById(role);
        }
        role.setCreated(LocalDateTime.now());
        return save(role);
    }

    @Override
    public List<Role> getRoleListByUserId(Integer userId) {
        return list(new QueryWrapper<Role>().inSql("id", "select role_id from t_user_role where user_id = " + userId));
    }

}
